package com.basil.gym.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.basil.gym.listener.CourseExcelListener;
import com.basil.gym.pojo.entity.Course;
import com.basil.gym.mapper.CourseMapper;
import com.basil.gym.pojo.query.CourseQuery;
import com.basil.gym.pojo.vo.CourseExcelVO;
import com.basil.gym.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basil.gym.util.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Basil
 * @since 2025-04-15
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public IPage<Course> selectByPage(CourseQuery courseQuery) {
        IPage<Course> page = new Page<>(courseQuery.getPage(), courseQuery.getLimit());
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(courseQuery.getName()), "name", courseQuery.getName());
        queryWrapper.eq(!ObjectUtils.isEmpty(courseQuery.getCoachId()), "coach_id", courseQuery.getCoachId());
        queryWrapper.like(!ObjectUtils.isEmpty(courseQuery.getRoomId()), "room_id", courseQuery.getRoomId());
        queryWrapper.ge(!ObjectUtils.isEmpty(courseQuery.getBeTime()), "time", courseQuery.getBeTime());
        queryWrapper.le(!ObjectUtils.isEmpty(courseQuery.getEnTime()), "time", courseQuery.getEnTime());
        queryWrapper.ge(!ObjectUtils.isEmpty(courseQuery.getBeLength()), "length", courseQuery.getBeLength());
        queryWrapper.le(!ObjectUtils.isEmpty(courseQuery.getEnLength()), "length", courseQuery.getEnLength());
        queryWrapper.ge(!ObjectUtils.isEmpty(courseQuery.getMaxCount()), "max_count", courseQuery.getMaxCount());
        courseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<CourseExcelVO> list = courseMapper.selectExcelList();
        ArrayList<CourseExcelVO> excelList = new ArrayList<>();
        for (CourseExcelVO courseExcelVO : list) {
            CourseExcelVO vo = new CourseExcelVO();
            BeanUtils.copyProperties(courseExcelVO, vo);
            excelList.add(vo);
        }
        ExcelUtil.exportExcel(response, excelList, CourseExcelVO.class, "course");
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), CourseExcelVO.class, new CourseExcelListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
